package com.valdemar.cookerybook.dao;

import java.util.Hashtable;
import java.util.List;

import javax.persistence.EntityManager;
import javax.persistence.Query;

import com.valdemar.cookerybook.model.TranslatableEntity;

public class BaseEntityHelperDao
{
	public static <T extends TranslatableEntity> List<T> listAll(int languageId, Class<T> entity)
	{
		EntityManager em = EMFService.get().createEntityManager();
		
		Query q = em.createQuery("select c from " + entity.getCanonicalName() + " c");
		
		List<T> entities = q.getResultList();
		
		Hashtable<String, String> translations = TranslationDao.instance.getTranslations( entity.getCanonicalName(), languageId);
		
		TranslationDao.instance.mergeTranslationsWithEntity(entities, translations);
		
		return entities;
	}
}
